package com.ems.service.impl;

import com.ems.entity.OrderEntity;
import com.ems.mapper.OrderMapper;
import com.ems.service.IOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ems.util.SnowflakeIdWorker;
import com.ems.util.StateEnum;
import com.ems.util.StateException;
import com.ems.vo.OrderVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 工单 服务实现类
 * </p>
 * @author WuZH
 * @since 2023-11-04
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, OrderEntity> implements IOrderService {

    @Resource
    private OrderMapper orderMapper;

    @Override
    public List<OrderVo> queryList (OrderVo orderVo) {
        if ( orderVo == null ) {
            throw new StateException ( StateEnum.REQUEST_PARAM_EXCE );
        }
        orderVo.setPage ( ( orderVo.getPage () - 1 ) * orderVo.getLimit () );
        List<OrderVo> orderVos = orderMapper.queryList ( orderVo );
        return orderVos;
    }

    @Override
    public Integer queryCount (OrderVo orderVo) {
        if ( orderVo == null ) {
            throw new StateException ( StateEnum.REQUEST_PARAM_EXCE );
        }
        Integer count = orderMapper.queryCount ( orderVo );
        return count;
    }

    @Override
    public OrderVo saveSingle (OrderVo orderVo) {
        if ( orderVo == null ) {
            throw new StateException ( StateEnum.REQUEST_PARAM_EXCE );
        }
        OrderEntity orderEntity = new OrderEntity ();
        BeanUtils.copyProperties ( orderVo, orderEntity );
        orderEntity.setId ( SnowflakeIdWorker.getInstance ().nextStringId () );
        orderEntity.setCreateTime ( new Date () );
        int insert = baseMapper.insert ( orderEntity );
        if ( insert == 0 ) {
            throw new StateException ( StateEnum.INSERT_EXCE );
        }
        return orderVo;
    }

    @Override
    public OrderVo querySingle (OrderVo orderVo) {
        orderVo = orderMapper.querySingle ( orderVo );
        return orderVo;
    }

}
